package com.example.dllo.my36kr.my.collect;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.my.MyUser;
import com.example.dllo.my36kr.news.NewsAdapter;
import com.example.dllo.my36kr.news.NewsBean;
import com.example.dllo.my36kr.news.newsecond.SecondActivity;
import com.example.dllo.my36kr.news.newsecond.SecondBean;
import com.example.dllo.my36kr.tools.BaseAty;
import com.example.dllo.my36kr.tools.DBTool;
import com.example.dllo.my36kr.tools.NetTool;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by dllo on 16/8/30.
 */
public class CollectAty extends BaseAty {

    private ListView listView;
    private CollectAdapter adapter;


    @Override
    protected int setLayout() {
        return R.layout.aty_collect;
    }

    @Override
    protected void initView() {

        listView = bindView(R.id.lv_aty_collect);
        adapter = new CollectAdapter(this);
        Button bactBnt = bindView(R.id.bnt_aty_collect_back);
        bactBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
//        final MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
//        final ArrayList<CollectBean>arrayList = new ArrayList<>();
//        BmobQuery<CollectUser> query = new BmobQuery<>();
//        query.addWhereEqualTo("name", myUser.getUsername());
//        query.setLimit(20);
//        query.findObjects(new FindListener<CollectUser>() {
//            @Override
//            public void done(final List<CollectUser> list, BmobException e) {
//                if (e == null){
//                    for (CollectUser collectUser : list){
//
//                        mNetTool.getNetData(collectUser.getCollect(), CollectBean.class, new NetTool.NetListener<CollectBean>() {
//                            @Override
//                            public void onSuccess(CollectBean collectBean) {
//                                arrayList.add(collectBean);
//                                adapter.setBeen(arrayList);
//                                listView.setAdapter(adapter);
//                            }
//
//                            @Override
//                            public void onError(String errorMsg) {
//
//                            }
//                        });
//                    }
//
//
//                }else {
//                    Toast.makeText(CollectAty.this, "888", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
        final BmobUser bmobUser = BmobUser.getCurrentUser(BmobUser.class);
            DBTool.getInstance().queryDB("xM = ?", bmobUser.getUsername(), new DBTool.QueryListener() {
                @Override
                public void onQueryComplete(final ArrayList<CollectBean> nativeBeen) {
                    adapter.setBeen(nativeBeen);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(CollectAty.this, SecondActivity.class);
                            intent.putExtra("details",String.valueOf(nativeBeen.get(position).getPostId()));
                            startActivity(intent);
                        }
                    });
                }
            });



    }
}
